#!"/Applications/XAMPP/xamppfiles/bin/perl"
print "Content-Type: text/html\n\n";

use lib '/Library/Perl/5.12/';
use strict;
use warnings;

use models::User;
use models::Pizza;
use models::Order;
use DataBaseManager;
use Data::Dumper;
use JSON;
use CGI;

# Connecting to dabase;
my $dbm = new DataBaseManager();
$dbm->connect();

my $cgi = new CGI;
my %vars = $cgi->Vars;
##print Dumper(%vars);
#print $cgi->header('text/plain');
#print $cgi->request_method;
#if ('POST' eq $cgi->request_method) 
#	{
#		print "{result:POST}";
#	}
#my %orders = getAllOrders();
#foreach my $supp (sort keys %orders) 
#		{
##			print Dumper($supp).Dumper($orders{$supp}{'id'});
#			$dbm->updateOrder($orders{$supp}{'id'});
#		}
#print Dumper(getAllOrders())."asd";
my $user = getUser($vars{'userId'});
if ($vars{'operation'} eq 'register')
{
	if($vars{"phone"} =~ /^(\+359|0){1}[1-9]?\d{4,11}$/)
	{
		my %hash = ("result",register($vars{'username'},$vars{'password'},$vars{'address'},$vars{'phone'},$vars{'ordersAddress'}));
		print encode_json(\%hash);
	}
	else
	{
		print '{"error":"Entered phone is not valid phone number!"}';
	}
}elsif($vars{'operation'} eq 'login')
{
	print encode_json(login($vars{'username'},$vars{'password'}));
}elsif($vars{'operation'} eq 'getUser')
{
	print getUser($vars{'id'});
}elsif($vars{'operation'} eq 'updateUser')
{
	if($vars{"phone"} =~ /^(\+359|0){1}[1-9]?\d{4,11}$/)
	{
		my %hash = ("result",updateUser($vars{'username'},$vars{'password'},$vars{'address'},$vars{'phone'},$vars{'ordersAddress'},$vars{'id'}));
		print encode_json(\%hash);
	}
	else
	{
		print '{"error":"Entered phone is not valid phone number!"}';
	}
}elsif(defined($user->{password})&&$user->{password} eq $vars{'password'})
{
		if($vars{'operation'} eq 'getAllSupplements')
		{
			my %hash = getAllSupplements();
			print encode_json(\%hash);
		}elsif($vars{'operation'} eq 'getAllSupplements')
		{
			
		}elsif($vars{'operation'} eq 'getPizzaSupplementsById')
		{
			my %hash = getPizzaSupplementsById($vars{'id'});
			print encode_json(\%hash);
		}elsif($vars{'operation'} eq 'getUserPizzasByUserId')
		{
			my %hash = getUserPizzasByUserId($vars{'id'});
			print encode_json(\%hash);
		}elsif($vars{'operation'} eq 'getUserPizzasByUserId')
		{
			my %hash = getUserPizzasByUserId();
			print encode_json(\%hash);
		}elsif($vars{'operation'} eq 'setPizza')
		{
			my $pizza =  new Pizza($vars{'name'},$vars{'userId'},$vars{'price'},$vars{'userId'});
			print encode_json(setPizza($pizza));
		}elsif($vars{'operation'} eq 'getUserOrders')
		{
			my %hash = getUserOrders($vars{'id'});
			print encode_json(\%hash);
		}
		elsif($vars{'operation'} eq 'getAllOrders')
		{
			my %hash = getUserOrders();
			print encode_json(\%hash);
		}elsif($vars{'operation'} eq 'setOrder')
		{
			my $params = $vars{'json'};
			my $string = decode_json($params);
			my $pizza = new Pizza($vars{'name'},$vars{'userId'},$vars{'size'},$vars{'price'});
			my $pizzaId = setPizza($pizza);
			setPizzaSupplements($pizzaId,$vars{'json'});
			
			my $order =  new Order($pizzaId,$vars{'userId'},"baking","5","30",$vars{'orderAddress'});
#			print 'pizzaId'.$pizzaId.'userId'.$vars{'userId'}.'orderAddr'.$vars{'orderAddress'};
			my %hash = ("result",setOrder($order));
			
			print encode_json(\%hash);
		}
}else
{
	print '{"error":"Incorrect Login"}';
}

#UPDATE USER
#$user = $dbm->selectUser(5);
#$user->{_address}="deeeeba";
#$dbm->updateUser($user);
#print Dumper($user);

#print Dumper($usr);
#$usr->{_address} = "liulin";
#print $dbm->updateUser($usr);

#$dbm->setPizzaSupplements (1, $dbm->getAllSupplements); # This is working;
#print Dumper($dbm->getPizzaSupplementsById(2));

my $pizza = new Pizza("peperone",17,30,18.4);

#print $dbm->setPizza($pizza);
#print json($pizza);
#print Dumper($dbm->getPizzaSupplementsById(1));

#print Dumper($dbm->getUserPizzasByUserId(7));

#my $order = new Order(1,7,'baking',"25","30 min.");
#print $dbm->setOrder($order);

#print Dumper($dbm->getUserOrders(0));

# Disconnect database connection
$dbm->disconnect();

sub json{return encode_json({ %{ shift() } });}

sub register
{
	my $user = new User(shift,shift,shift,shift,shift);
	return $dbm->register($user)
}

sub login
{
	return $dbm->login(shift,shift);
}

sub getUser
{
	return $dbm->getUserById(shift);
}

sub updateUser
{
	my $user = new User(shift,shift,shift,shift,shift);
	$user->setId(shift);
	return $dbm->updateUser($user);
}

sub getAllSupplements
{
	return $dbm->getAllSupplements;
}
sub setPizzaSupplements
{	
	return $dbm->setPizzaSupplements(shift,shift);
}

sub getPizzaSupplementsById
{
	return $dbm->getPizzaSupplementsById(shift);
}

sub getUserPizzasByUserId
{
	return $dbm->getUserPizzasByUserId(shift);
}
sub setPizza
{
	return $dbm->setPizza(shift,shift,shift);
}
sub getUserOrders
{
	return $dbm->getUserOrders(shift);
}
sub getAllOrders
{
	return $dbm->getAllOrders;	
}
sub setOrder
{
	return $dbm->setOrder(shift);
}